package com.csy.commons.framework.security.service;

import com.csy.commons.core.utils.ServletUtils;
import com.csy.commons.dto.LoginUser;
import com.csy.commons.dto.SysRole;
import com.csy.commons.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.Set;

/**
 * 自定义权限校验规则

 */
@Component("pe")
public class PermsExpressionService {

    private static final String ALL_PERMISSION = "*:*:*";
    @Autowired
    private TokenService tokenService;

    /*
    * 验证用户是否具备某一权限
    * */
    public boolean hasPerms(String permission){
        if (StringUtils.isEmpty(permission)){
            return false;
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (Objects.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getPermissions())) {

            return false;
        }
        return hasPermissions(loginUser.getPermissions(),permission);
    }

    /*
    * 判断是否包含权限
    * */
    private boolean hasPermissions(Set<String> permissions, String permission) {

        return permissions.contains(permission) || permissions.contains(ALL_PERMISSION);
    }

    /*
    * 验证用户是否有任意一个权限
    * */
    public boolean hasAnyPerms(String permissions){
        if (StringUtils.isEmpty(permissions)){
            return false;
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (Objects.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getPermissions())) {
            return false;
        }
        Set<String> authorities = loginUser.getPermissions();
        for (String perms : permissions.split(",")){
            if (perms != null && hasPermissions(authorities,perms)){
                return true;
            }
        }
        return false;
    }

    /*
    * 判断用户是否拥有某个角色
    * */
    public boolean hasRole(String role){
        if (StringUtils.isEmpty(role)){
            return false;
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (Objects.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getUser().getRoles())) {
            return false;
        }
        for (SysRole sysRole: loginUser.getUser().getRoles()) {
            String roleKey = sysRole.getRoleKey();
            if ("admin".equals(roleKey) || roleKey.equals(role)){
                return true;
            }
        }
        return false;
    }

    /*
     * 判断用户是否拥有任意一个角色
     * */
    public boolean hasAnyRole(String roles){
        if (StringUtils.isEmpty(roles)){
            return false;
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (Objects.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getUser().getRoles())) {
            return false;
        }
        for (String role : roles.split(",")){
            if (hasRole(role)){
                return true;
            }
        }
        return false;
    }

}
